package com.hluck.criminalintent

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.hluck.criminalintent.database.CrimeRepository
import com.hluck.criminalintent.databinding.FragmentCrimeListBinding
import com.hluck.criminalintent.model.Crime
import kotlin.concurrent.thread

class CrimeListFragment : Fragment() {
    
    private val TAG: String = "CrimeListFragment"

    private val crimeListViewModel:CrimeListViewModel by viewModels()
    private var binding:FragmentCrimeListBinding? = null

    companion object{
        fun newInstance():CrimeListFragment{
            return CrimeListFragment()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate: crimesList size:${crimeListViewModel.crimes.size}")
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_crime_list, container, false)
        binding = DataBindingUtil.bind(view)
        binding?.rv?.layoutManager = LinearLayoutManager(context)
        binding?.rv?.adapter = CrimeAdapter(crimeListViewModel.crimes)

//        thread {
//            for (crime in crimeListViewModel.crimes) {
//                CrimeRepository.getInstance().insertCrime(crime)
//            }
//        }

        return binding?.root
    }

    private fun updateUI(crimes:List<Crime>){

    }

}